package com.ticket.sass.ticketserver.config

import com.ticket.sass.ticketserver.interceptor.RequestInterceptor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
class MyWebMvcConfigurer : WebMvcConfigurer {

    @Bean
    fun authenticationInterceptor(): RequestInterceptor {
        return RequestInterceptor()
    }


    /**
     * 添加拦截器
     */
    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(authenticationInterceptor())
            .excludePathPatterns("/error")

            // swagger
            .excludePathPatterns("/doc.html")
            .excludePathPatterns("/v2/api-docs")
            .excludePathPatterns("/webjars/**")
            .excludePathPatterns("/swagger-resources/**")
    }

    /**
     * 跨域 cors 配置
     */
    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**").allowedOrigins("*")
            .allowedMethods("*")
    }
}